package com.briup.server.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.briup.common.constant.SystemConstant;
import com.briup.common.constraints.Add;
import com.briup.common.constraints.Update;
import com.briup.common.response.Result;
import com.briup.server.domain.dto.UserModifiedDTO;
import com.briup.server.domain.dto.UserQueryDTO;
import com.briup.server.domain.dto.UserResetPasswordDTO;
import com.briup.server.domain.vo.UserDetailVO;
import com.briup.server.security.Permission;
import com.briup.server.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.groups.Default;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@Slf4j
@Tag(name = "用户管理模块")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @Permission(SystemConstant.ADMIN_ROLE_NAME)
    @Operation(summary = "根据条件分页查询所有用户")
    @GetMapping
    public Result<?> pageQuery(UserQueryDTO queryDTO) {
        Page<UserDetailVO> pageInfo = userService.pageQueryByCondition(queryDTO);
        return Result.success(pageInfo).apply("查询成功");
    }

    // 在分组校验情况下，如果存在没有指定分组的校验注解，
    // 则默认添加Default分组，确保所有校验注解都能被触发
    @Operation(summary = "更新用户信息")
    @PutMapping
    public Result<?> update(@RequestBody @Validated({Update.class, Default.class}) UserModifiedDTO modifiedDTO) {
        log.info("更新用户信息: {}", modifiedDTO);
        userService.saveOrUpdateUser(modifiedDTO);
        return Result.success(null).apply("更新成功");
    }

    @Permission(SystemConstant.ADMIN_ROLE_NAME)
    @Operation(summary = "新增用户")
    @PostMapping
    public Result<?> create(@RequestBody @Validated({Add.class, Default.class}) UserModifiedDTO modifiedDTO) {
        userService.saveOrUpdateUser(modifiedDTO);
        return Result.success(null).apply("新增成功");
    }

    @Permission(SystemConstant.ADMIN_ROLE_NAME)
    @Operation(summary = "删除用户信息（批量删除和单个删除）")
    @DeleteMapping
    public Result<?> delete(@RequestBody
                            @NotEmpty(message = "删除用户ID不能为空") List<Long> ids) {
        userService.removeUser(ids);
        return Result.success(null).apply("删除成功");
    }

    @Permission(SystemConstant.ADMIN_ROLE_NAME)
    @Operation(summary = "管理员重置用户密码")
    @PutMapping("/reset/{userId}")
    public Result<?> resetPassword(@PathVariable("userId") Long userId) {
        userService.resetPassword(userId);
        return Result.success(null).apply("重置成功");
    }

    @Operation(summary = "修改用户密码（用户自己修改）")
    @PutMapping("/password")
    public Result<?> updatePassword(@RequestBody @Valid
                                    UserResetPasswordDTO resetPasswordDTO) {
        userService.updatePassword(resetPasswordDTO);
        return Result.success(null).apply("更新成功");
    }
}
